package com.xposed.aliproxy.thread;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.xposed.aliproxy.http.HttpManager;
import com.xposed.aliproxy.utils.AlertDialogUtil;
import com.xposed.aliproxy.utils.JsonUtil;

import org.json.JSONObject;

public class CheckNewVersionThread extends Thread {
    private Activity activity;

    public CheckNewVersionThread(Activity activity) {
        this.activity = activity;
    }

    public void run() {
        super.run();
        try {
            HttpManager.requestUpdate(this.activity, new HttpManager.RequestCallback() {
                @Override
                public void onFinish() {
                }

                @Override
                public void onFailMsg(String respCode, String respMsg) {
                }

                @Override
                public void onSuccess(JSONObject jSONObject) {
                    String a = JsonUtil.getString(jSONObject, "message");
                    final String a2 = JsonUtil.getString(jSONObject, "url");
                    AlertDialogUtil.showAlertDialog(CheckNewVersionThread.this.activity, "版本更新", String.format("\n更新内容：%s\n", new Object[]{a}), "更新", null, new OnClickListener() {
                        public void onClick(View view) {
                            CheckNewVersionThread.this.activity.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(a2)));
                            Toast.makeText(CheckNewVersionThread.this.activity, "程序下载中，下载完成后请手动更新!", Toast.LENGTH_LONG).show();
                        }
                    }, null);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
